<template>
  <div class="app-container">
    <el-form :model="query" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="支部名称">
        <el-input v-model="query.name" placeholder="请输入党支部名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="handleReset">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
        <help></help>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="序号" align="center" width="50" type="index" fixed="left" />
      <el-table-column label="党支部名称" align="center" prop="name" />
      <el-table-column label="负责人" align="center" prop="administrator" />
      <el-table-column label="负责人电话" align="center" prop="administratorPhone" />
      <el-table-column label="成立时间" align="center">
        <template slot-scope="scope">
          {{ Number(scope.row.establishDate) | moment }}
        </template>
      </el-table-column>
      <el-table-column label="登记日期" align="center">
        <template slot-scope="scope">
          {{ Number(scope.row.registerDate) | moment }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="handleInfo(scope.row)">详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:party:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />

    <help-dialog title="党支部管理" :visible.sync="dialogVisible">
      <el-form size="small" ref="queryParam" :model="queryParam" label-width="120px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="党支部名称" prop="name">
              <el-input v-model="queryParam.name" placeholder="请输入党支部名称" />
            </el-form-item>
            <el-form-item label="管理人" prop="administrator">
              <el-input v-model="queryParam.administrator" placeholder="请输入管理人" />
            </el-form-item>
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="queryParam.contacts" placeholder="请输入联系人" />
            </el-form-item>
            <el-form-item label="成立时间" prop="establishDate">
              <el-date-picker
                v-model="queryParam.establishDate"
                type="date"
                placeholder="选择日期"
                value-format="timestamp"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级党支部">
              <el-select v-model="queryParam.leaderId" style="width: 100%">
                <el-option v-for="item in partyTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="管理人电话" prop="administratorPhone">
              <el-input v-model="queryParam.administratorPhone" />
            </el-form-item>
            <el-form-item label="联系人电话" prop="contactsPhone">
              <el-input v-model="queryParam.contactsPhone" placeholder="请输入联系人电话" />
            </el-form-item>
            <el-form-item label="登记时间" prop="registerDate">
              <el-date-picker
                v-model="queryParam.registerDate"
                type="date"
                placeholder="选择日期"
                value-format="timestamp"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介">
              <el-input type="textarea" :rows="3" v-model="queryParam.content" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="添加组织附件" prop="enclosure">
              <y-upload :path="[queryParam.enclosure]" limit="1" @onSuccess="onSuccess" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </help-dialog>
  </div>
</template>

<script>
import branchApi from '@/api/party/branch'

export default {
  data() {
    return {
      loading: false,
      query: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },
      total: 0,
      list: [],
      corpList: [],
      partyTypeList: [],
      dialogVisible: false,
      queryParam: {},
      rules: {
        name: [{ required: true, message: '请输入党支部名称', trigger: 'blur' }],
        administrator: [{ required: true, message: '请输入管理人', trigger: 'blur' }],
        contacts: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        establishDate: [{ required: true, message: '选择日期', trigger: 'blur' }],
        administratorPhone: [{ required: true, message: '请输入管理人电话', trigger: 'blur' }],
        contactsPhone: [{ required: true, message: '请输入联系人电话', trigger: 'blur' }],
        registerDate: [{ required: true, message: '选择日期', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    this.getPartyList()
  },
  methods: {
    getList() {
      this.loading = true
      branchApi.branchList(this.query).then((res) => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleReset() {
      this.query = {
        projectName: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.queryParam = {}
      this.dialogVisible = true
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
    },
    onSuccess(file) {
      this.queryParam.enclosure = file.router
    },
    handleInfo(row) {
      branchApi.branchDetail(row.id).then((res) => {
        this.queryParam = res.data

        this.dialogVisible = true
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('确认删除吗？')
        .then(function () {
          return branchApi.delParty(row.id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    getPartyList() {
      branchApi.partyList().then((res) => {
        this.partyTypeList = res.rows
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['queryParam'].validate((valid) => {
        if (valid) {
          if (this.queryParam.id != null) {
            branchApi.updateParty(this.queryParam).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.dialogVisible = false
              this.getList()
            })
          } else {
            branchApi.addParty(this.queryParam).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.dialogVisible = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
